package com.teamone.etl.enterprise.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.enterprise.dao.EnterpriseDao;
import com.teamone.etl.enterprise.entity.Enterprise;
import com.teamone.etl.enterprise.request.EnterpriseRequest;
import com.teamone.etl.enterprise.response.ApiResponse;
import com.teamone.etl.enterprise.service.EnterpriseService;
import com.teamone.etl.exception.CommonCode;
import com.teamone.etl.exception.CustomException;
import com.teamone.etl.util.HmacSha1Util;
import com.teamone.etl.util.OssUtil;
import com.teamone.etl.util.Sha256Util;
import com.teamone.etl.util.VerifyUtil;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import static com.teamone.etl.constant.EtlConstant.MySqlConstant.MAX_PAGE_SIZE;

/**
 (Enterprise)表服务实现类

 @author makejava
 @since 2021-01-19 16:31:59 */
@Service("enterpriseService")
@Transactional(rollbackFor = Exception.class)
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseDao, Enterprise> implements EnterpriseService {

    @Override
    public PageInfo<Enterprise> getList (EnterpriseRequest request) {
        PageHelper.startPage(request.getPageNum(),
                request.getPageSize()>MAX_PAGE_SIZE?MAX_PAGE_SIZE:request.getPageSize());
        List<Enterprise> list = baseMapper.getList(request);
        return new PageInfo<>(list);
    }

    @Override
    public int updateWithSql (EnterpriseRequest request) throws CustomException {
        //校验手机号格式
        boolean flag = request.getRegistrantMobileNumber() != null
                && VerifyUtil.phone(request.getRegistrantMobileNumber().toString());
        if(!flag){
            throw new CustomException(CommonCode.ENTERPRISE_MOBILE_INVALID);
        }
        //判断手机号是否已经被注册
        if(baseMapper.getByMobileAndIdIsNotThis(request)!=null){
            throw new CustomException(CommonCode.MOBILE_ALREADY_EXSISTS);
        }
        //判断企业名称是否已经注册
        Enterprise byEnterpriseNameAndIdIsNotThis = baseMapper.getByEnterpriseNameAndIdIsNotThis(request);
        if(byEnterpriseNameAndIdIsNotThis!=null){
            throw new CustomException(CommonCode.ENTERPRISE_EXISTS);
        }
        request.setUpdateTime(new Date());
        //校验
        request.verify2();
        return baseMapper.update(request);
    }

    @Override
    public int add (EnterpriseRequest request) throws CustomException {
        //判断手机号是否已经被注册
        if(baseMapper.getByMobile(request.getRegistrantMobileNumber())!=null){
            throw new CustomException(CommonCode.MOBILE_ALREADY_EXSISTS);
        }
        request.init();
        //校验手机号+密码
        request.verify1();
        request.setPassword(Sha256Util.encrypt(request.getPassword()));
        //flag判断 不为空是管理员添加 详细校验
        if(request.getFlag()!=null){
            request.verify2();
            //用户自己注册不会有企业名称
            Enterprise byEnterpriseName = getByEnterpriseName(request.getEnterpriseName());
            if(byEnterpriseName!=null){
                throw new CustomException(CommonCode.ENTERPRISE_EXISTS);
            }
        }
        return baseMapper.add(request);
    }

    @Override
    public String uploadLicenseImage (MultipartFile file) throws CustomException {
        return OssUtil.uploadFileInputSteam("license-img-", file);
    }

    @Override
    public String uploadEnterpriseImage (MultipartFile file) throws CustomException {
        return OssUtil.uploadFileInputSteam("enterprise-img-", file);
    }

    /**
     接口列表
     @return 接口列表
     @param enterpriseId 企业id
     */
    @Override
    public List<ApiResponse> apiList (Integer enterpriseId) {
        List<ApiResponse> apis = baseMapper.apiList(enterpriseId);
        apis.forEach(api -> api.setFailedcount(api.getUseamount()-api.getSuccesscount()));
        return apis;
    }

    @Override
    public boolean login (EnterpriseRequest request) throws CustomException {
        Enterprise login = baseMapper.getByMobile(request.getRegistrantMobileNumber());
        if(login!=null){
            if(login.getPassword().equals(HmacSha1Util.encrypt(request.getPassword()))){
                return true;
            }
            throw new CustomException(CommonCode.PASSWORD_ERROR);
        }
        throw new CustomException(CommonCode.NOT_REGIST);
    }

    @Override
    public boolean changePassword (EnterpriseRequest request) throws CustomException {
        //旧密码
        String old1 = request.getOldPassword1();
        //判断两次旧密码是否一致
        if(old1.equals(request.getOldPassword2())){
            //根据手机号获取信息
            Enterprise byMobile = baseMapper.getByMobile(request.getRegistrantMobileNumber());
            //对比密码
            if(Sha256Util.encrypt(old1).equals(byMobile.getPassword())){
                request.setPassword(Sha256Util.encrypt(request.getNewPassword()));
                baseMapper.changePassword(request);
                return true;
            }
            throw new CustomException(CommonCode.OLD_PASSWORD_INVALID);
        }
        throw new CustomException(CommonCode.OLD_OLD_PASSWORD_INVALID);
    }

    @Override
    public Enterprise getByEnterpriseName (String enterpriseName) {
        return baseMapper.getByEnterpriseName(enterpriseName);
    }

    @Override
    public List<Enterprise> getAll () {
        return baseMapper.getAll();
    }

}